package top.wshape1.shiziapi.message.config;

import jakarta.annotation.Resource;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import top.wshape1.shiziapi.message.context.MachineNodeContextHandler;
import top.wshape1.shiziapi.message.listener.MessageListener;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-09-18
 */

@Configuration
public class RabbitMqConfig {

    /*
    EXCHANGE
     */
    public static final String SHIZIAPI_MESSAGE_USER_EXCHANGE = "shiziapi-message-user-exchange";

    public static final String SHIZIAPI_MESSAGE_BROADCAST_EXCHANGE = "shiziapi-message-broadcast-exchange";

    public static final String SHIZIAPI_MESSAGE_USER_BINDING_PREFIX = "shiziapi.message.user.";

    @Value("${spring.rabbitmq.listener.simple.acknowledge-mode:manual}")
    private AcknowledgeMode acknowledgeMode;

    @Lazy
    @Resource
    private SimpleRabbitListenerContainerFactory containerFactory;

    @Resource
    private MessageListener messageListener;

    @Resource
    private AmqpAdmin amqpAdmin;

    @Resource
    private MachineNodeContextHandler machineNodeContextHandler;


    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange shiziapiMessageUserExchange() {
        return new TopicExchange(SHIZIAPI_MESSAGE_USER_EXCHANGE);
    }

    @Bean
    public Exchange shiziapiMessageBroadcastExchange() {
        return new FanoutExchange(SHIZIAPI_MESSAGE_BROADCAST_EXCHANGE);
    }

    @Bean
    public MessageListenerContainer userMessageListenerContainer() {
        // 创建队列
        Queue declareQueue = amqpAdmin.declareQueue();
        // 绑定队列
        if (declareQueue != null) {
            amqpAdmin.declareBinding(new Binding(
                    declareQueue.getName(),
                    Binding.DestinationType.QUEUE,
                    SHIZIAPI_MESSAGE_USER_EXCHANGE,
                    SHIZIAPI_MESSAGE_USER_BINDING_PREFIX + machineNodeContextHandler.MACHINE_ID,
                    null));
        }

        // 设置监听消费者
        SimpleMessageListenerContainer container = containerFactory.createListenerContainer();
        container.setQueues(declareQueue);
        container.setAcknowledgeMode(acknowledgeMode);
        container.setExposeListenerChannel(true);
        container.setConsumerTagStrategy(queue -> queue + "_" + machineNodeContextHandler.MACHINE_ID);
        container.setMessageListener(messageListener.listenMessageUserExchange());
        return container;
    }

}
